package net.ilikecoffee.myownsqlclient.app;
import java.sql.SQLException;
import java.util.Arrays;

import net.ilikecoffee.myownsqlclient.models.Query;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Main {

	static Main instance = null;
	
	private Display display;
	private	Font fixedFont;
	private Shell shell;
	private CTabFolder tabFolder;
	private Text status;
	private int tabCount;
	private String prefixShellText = "MyOwnSqlBrowser - ";
	
	private Main() {
		display = new Display();
		fixedFont = new Font(display, "Monospace", 10, 0);
		shell = new Shell(display);
		shell.setText("MyOwnSqlBrowser ");
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		shell.setLayout(layout);
		initMenu();
		initTabFolder();
		initConnectionBar();
	}

	private void initConnectionBar() {
		status = new Text(shell, SWT.NONE);
		status.setText(Query.getStatus());
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		status.setLayoutData(gridData);
		shell.setText(prefixShellText + Query.getStatus());
		status.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				Query.setUrlConn(status.getText());
			}
			
			@Override
			public void focusGained(FocusEvent e) {
			}
		});
	}

	private void initMenu() {
		Menu bar = new Menu (shell, SWT.BAR);
		shell.setMenuBar(bar);
		MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
		fileItem.setText("&File");
		Menu fileSubmenu = new Menu(shell, SWT.DROP_DOWN);
		fileItem.setMenu(fileSubmenu);
		
		MenuItem newTabItem = new MenuItem (fileSubmenu, SWT.PUSH);
		newTabItem.addListener (SWT.Selection, new Listener () {
			public void handleEvent (Event e) {
				addTab();
			}
		});
		newTabItem.setText("New &Tab\tCtrl+T");
		newTabItem.setAccelerator(SWT.MOD1 | 'T');

		MenuItem queryItem = new MenuItem(bar, SWT.CASCADE);
		queryItem.setText("&Query");
		Menu querySubmenu = new Menu(shell, SWT.DROP_DOWN);
		queryItem.setMenu(querySubmenu);

		MenuItem executeItem = new MenuItem (querySubmenu, SWT.PUSH);
		executeItem.addListener (SWT.Selection, new Listener () {
			public void handleEvent (Event e) {
				executeQuery();
			}
		});
		executeItem.setText("Execute Query\tCtrl+Intro");
		executeItem.setAccelerator(SWT.MOD1 | SWT.CR);
		
		MenuItem helpItem = new MenuItem(bar, SWT.NONE);
		helpItem.setText("&Help");		
	}

	private void initTabFolder() {
		tabFolder = new CTabFolder(shell, SWT.CLOSE | SWT.BORDER);
		
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		tabFolder.setLayoutData(gridData);
		
		tabFolder.setSimple(true);		
		tabCount = 1;
		addTab();
	}
	
	private void executeQuery() {
		Text queryBox = (Text)tabFolder.getSelection().getData("queryBox");
		Text resultBox = (Text)tabFolder.getSelection().getData("resultBox");
		try {
			resultBox.setText("");
			Query query = new Query(queryBox.getText());
			for (String[] s : query.getResults()) {
				resultBox.append(Arrays.toString(s)+'\n');
			}
		} catch (SQLException e1) {
			resultBox.setText(e1.getMessage());
		}
		status.setText(Query.getStatus());
		shell.setText(prefixShellText + Query.getStatus());
	}
	
	private void addTab() {
		
		CTabItem i = new CTabItem(tabFolder, SWT.NULL);
		i.setText("Query " + tabCount++);
		
		Composite c = new Composite(tabFolder, 0);
		c.setLayout(new FillLayout(SWT.VERTICAL));
		
		Text queryBox = new Text(c,SWT.MULTI|SWT.BORDER | SWT.V_SCROLL | SWT.WRAP);
		queryBox.setFont(fixedFont);
		
		Text resultBox = new Text(c,SWT.MULTI|SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		resultBox.setFont(fixedFont);
		
		i.setData("queryBox",queryBox);
		i.setData("resultBox",resultBox);
		i.setControl(c);
		tabFolder.setSelection(i);
	}
	
	public static Main getInstance() {
		if (instance == null) {
			instance = new Main();
		}
		return instance;
	}
	
	public static void main(String[] args) {
		
		if (args.length > 0) {
			Query.setUrlConn(args[0]);
		}
		
		Main m = getInstance();		
		m.shell.open();
		while (!m.shell.isDisposed()) {
			if (!m.display.readAndDispatch()) m.display.sleep();
		}
		m.display.dispose();
	}
}
